//
//  BaseRecommend.m
//  AutoHome
//
//  Created by wqh on 15-5-10.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "BaseRecommend.h"

#import "RecommendModel.h"
#import "RecommendCell.h"
#import "AFNetworking.h"
#import "QFRefreshView.h"




@interface BaseRecommend ()
{
    AFHTTPRequestOperationManager *_afManager;
}
@end

@implementation BaseRecommend

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentPage = 1;
    self.lastId = @"0";
    self.lasttime = @"0";
    self.isRefreshing = NO;
    self.dataArr = [NSMutableArray array];
//    [self creatRefreshView];
}

- (void)createRefreshView {
    
    //下拉刷新
    __weak typeof(self) mySelf = self;
    [self.tableView setPullDownHandler:^(QFRefreshView *refreshView) {
        if (mySelf.isRefreshing) {
            return ;
        }
        mySelf.isRefreshing = YES;
        mySelf.lastId = @"0";
        mySelf.lasttime = @"0";
        mySelf.currentPage = 1;
        
        [mySelf downloadData];
    }];
    
    //上拉加载更多
    [self.tableView setPullUpHandler:^(QFRefreshView * refreshView) {
        if (mySelf.isRefreshing) {
            return ;
        }
        mySelf.isRefreshing = YES;

        if (mySelf.dataArr) {
            RecommendModel *model = [mySelf.dataArr lastObject];
           
            mySelf.lastId = model.lastid;
         
            mySelf.lasttime = model.lasttime;
            
            mySelf.currentPage = mySelf.currentPage +1;
        }
        
        [mySelf downloadData];
    }];
}

- (void)downloadData {
    
    _afManager = [AFHTTPRequestOperationManager manager];
    _afManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *url = @"";
    if ([self.info[@"mark"] isEqualToString:@"1"]) {
      
        url = [NSString stringWithFormat:self.info[@"url"],self.lastId];
       
    }else if([self.info[@"mark"] isEqualToString:@"2"]) {
        url = [NSString stringWithFormat:self.info[@"url"],self.currentPage,self.lasttime];
    }
    [_afManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self downloadDataSuccess:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)downloadDataSuccess:(id)responseObject {
    if ([self.lasttime isEqualToString:@"0"] || [self.lastId isEqualToString:@"0"]) {
        [self.dataArr removeAllObjects   ];
    }
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dict[@"result"][@"newslist"];
    if (array == nil) {
        array = dict[@"result"][@"list"];
    }
    for (NSDictionary *dictModel in array) {
        RecommendModel *model = [[RecommendModel alloc] init];
        [model setValuesForKeysWithDictionary:dictModel];
        [self.dataArr addObject:model];
    }
    [self.tableView reloadData];
    self.isRefreshing = NO;
    [self.tableView stopFooterViewLoading];
    [self.tableView stopHeaderViewLoading];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64-49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:RecommendCellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RecommendCell" owner:self options:nil] lastObject];
    }
    [cell showDataWithModel:self.dataArr[indexPath.row] type:NO];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    RecommendModel *model = self.dataArr[indexPath.row];
    if ([model.mediatype.stringValue isEqualToString:@"6"]) {
        return 110;
    }
    return 78;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
